package com.ruoyi.web.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.admin.domain.vo.request.waybill.WaybillAddRequest;
import com.ruoyi.admin.domain.vo.request.waybill.WaybillPageRequest;
import com.ruoyi.admin.domain.vo.request.waybill.WaybillUpdateRequest;
import com.ruoyi.admin.domain.vo.response.waybill.WaybillPageResponse;
import com.ruoyi.admin.domain.vo.response.waybill.WaybillResponse;
import com.ruoyi.admin.service.WaybillService;
import com.ruoyi.common.api.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 快递(Waybill)Controller类
 *
 * @author jianhan
 * @date 2022-12-07 15:41:41
 */
@Api(value = "后台快递 Controller", tags = {"后台快递"})
@RestController
@RequestMapping("/api/waybill")
public class WaybillController {

    @Autowired
    private WaybillService waybillService;

    /**
     * 根据id获取数据
     *
     * @author jianhan
     * @date 2022-12-07 15:41:41
     **/
    @ApiOperation(value = "根据id获取数据", notes = "根据id获取数据")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/{id}")
    public Result<WaybillResponse> get(@PathVariable Long id) {
        return waybillService.getId(id);
    }


    /**
     * 分页获取快递列表
     *
     * @author jianhan
     * @date 2022-12-07 15:41:41
     **/
    @ApiOperation(value = "分页获取快递列表", notes = "分页获取快递列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页码", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "查询数量", paramType = "query", dataType = "Integer")
    })
    @GetMapping
    public Result<WaybillPageResponse> pageList(@Valid WaybillPageRequest req) {
        return waybillService.page(new Page<>(req.getPageNum(), req.getPageSize()), req.getWaybillModel());
    }


    /**
     * 添加快递
     *
     * @author jianhan
     * @date 2022-12-07 15:41:41
     **/
    @ApiOperation(value = "添加快递", notes = "添加快递")
    @PostMapping
    public Result add(@RequestBody @Valid WaybillAddRequest req) {
        return waybillService.add(req.getWaybillModel());
    }


    /**
     * 修改快递
     *
     * @author jianhan
     * @date 2022-12-07 15:41:41
     **/
    @ApiOperation(value = "修改快递", notes = "修改快递")
    @PutMapping()
    public Result update(@RequestBody @Valid WaybillUpdateRequest req) {
        return waybillService.update(req.getWaybillModel());
    }

    /**
     * @description:  本月损耗
     * @param:
     * @return: monthlyLossVo
     * @author: jianhan
     * @date: 2023/2/9 16:11
     **/
    @ApiOperation(value = "本月损耗", notes = "本月损耗")
    @GetMapping("/lose")
    public Result lose() {
        return waybillService.lose();
    }

    /**
     * 删除快递
     *
     * @author jianhan
     * @date 2022-12-07 15:41:41
     **/
    @ApiOperation(value = "删除快递", notes = "删除快递")
    @ApiImplicitParam(name = "ids", value = "id集合,逗号隔开", required = true, paramType = "query", dataType = "String")
    @DeleteMapping("/{ids}")
    public Result update(@PathVariable String ids) {
        Boolean result = waybillService.removeByIds(Arrays.asList(ids.split(",")).stream().collect(Collectors.toList()));
        return Result.judge(result);
    }

}
